
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>nirvana · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="config.html" />
    
    
    <link rel="prev" href="service.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    概览
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    快速入门
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../guides/project.html">
            
                <a href="../guides/project.html">
            
                    
                    项目结构和初始化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../guides/api.html">
            
                <a href="../guides/api.html">
            
                    
                    API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../guides/doc.html">
            
                <a href="../guides/doc.html">
            
                    
                    生成 API 文档
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../guides/client.html">
            
                <a href="../guides/client.html">
            
                    
                    生成客户端
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    概念
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../concepts/cp.html">
            
                <a href="../concepts/cp.html">
            
                    
                    Consumer && Producer
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../concepts/operator.html">
            
                <a href="../concepts/operator.html">
            
                    
                    Operator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../concepts/filter.html">
            
                <a href="../concepts/filter.html">
            
                    
                    Filter
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../concepts/prefab.html">
            
                <a href="../concepts/prefab.html">
            
                    
                    Prefab
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../concepts/modifier.html">
            
                <a href="../concepts/modifier.html">
            
                    
                    Modifier
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../concepts/middleware.html">
            
                <a href="../concepts/middleware.html">
            
                    
                    Middleware
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="../concepts/context.html">
            
                <a href="../concepts/context.html">
            
                    
                    Context
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="../concepts/source.html">
            
                <a href="../concepts/source.html">
            
                    
                    Parameter Generator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="../concepts/destination.html">
            
                <a href="../concepts/destination.html">
            
                    
                    Destination Handler
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.10" data-path="../concepts/error.html">
            
                <a href="../concepts/error.html">
            
                    
                    错误包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.11" data-path="../concepts/method.html">
            
                <a href="../concepts/method.html">
            
                    
                    方法包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.12" data-path="../concepts/configurer.html">
            
                <a href="../concepts/configurer.html">
            
                    
                    配置器机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.13" data-path="../concepts/plugin.html">
            
                <a href="../concepts/plugin.html">
            
                    
                    插件机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.14" data-path="../concepts/clients.html">
            
                <a href="../concepts/clients.html">
            
                    
                    多客户端整合
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    插件
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../plugins/logger.html">
            
                <a href="../plugins/logger.html">
            
                    
                    系统日志插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../plugins/reqlog.html">
            
                <a href="../plugins/reqlog.html">
            
                    
                    请求日志插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../plugins/metrics.html">
            
                <a href="../plugins/metrics.html">
            
                    
                    监控指标插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../plugins/tracing.html">
            
                <a href="../plugins/tracing.html">
            
                    
                    请求追踪插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../plugins/profiling.html">
            
                <a href="../plugins/profiling.html">
            
                    
                    性能分析插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../plugins/version.html">
            
                <a href="../plugins/version.html">
            
                    
                    版本信息插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../plugins/healthcheck.html">
            
                <a href="../plugins/healthcheck.html">
            
                    
                    健康检查插件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    框架开发者指南
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="start.html">
            
                <a href="start.html">
            
                    
                    准备工作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="log.html">
            
                <a href="log.html">
            
                    
                    log
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="errors.html">
            
                <a href="errors.html">
            
                    
                    errors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="definition.html">
            
                <a href="definition.html">
            
                    
                    definition
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="method.html">
            
                <a href="method.html">
            
                    
                    definition/method
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="converter.html">
            
                <a href="converter.html">
            
                    
                    operators/converter
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="validator.html">
            
                <a href="validator.html">
            
                    
                    operators/validator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.8" data-path="router.html">
            
                <a href="router.html">
            
                    
                    service/router
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9" data-path="service.html">
            
                <a href="service.html">
            
                    
                    service
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.5.10" data-path="nirvana.html">
            
                <a href="nirvana.html">
            
                    
                    nirvana
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.11" data-path="config.html">
            
                <a href="config.html">
            
                    
                    config
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.12" data-path="rest.html">
            
                <a href="rest.html">
            
                    
                    rest
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.13" data-path="utils.html">
            
                <a href="utils.html">
            
                    
                    utils
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.14" data-path="command.html">
            
                <a href="command.html">
            
                    
                    cmd/nirvana
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >nirvana</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="nirvana-&#x5305;">nirvana &#x5305;</h1>
<p>nirvana &#x5305;&#x5728;&#x6839;&#x76EE;&#x5F55;&#x4E2D;&#xFF0C;&#x5B9E;&#x73B0;&#x4E86; Nirvana Server &#x548C;&#x63D2;&#x4EF6;&#x7CFB;&#x7EDF;&#x3002;&#x8FD9;&#x4E2A;&#x5305;&#x653E;&#x7F6E;&#x5728;&#x6839;&#x76EE;&#x5F55;&#x4E2D;&#x662F;&#x56E0;&#x4E3A;&#x8FD9;&#x4E2A;&#x5305;&#x662F; Nirvana &#x63D0;&#x4F9B;&#x7684;&#x7528;&#x4E8E;&#x751F;&#x6210; API Server &#x7684;&#x9876;&#x7EA7;&#x5305;&#xFF0C;&#x800C;&#x4E14;&#x5176;&#x4F9D;&#x8D56;&#x7684;&#x6240;&#x6709;&#x5305;&#x53EA;&#x6765;&#x81EA; Nirvana &#x81EA;&#x8EAB;&#x548C;&#x6807;&#x51C6;&#x5E93;&#xFF08;config &#x5305;&#x4F9D;&#x8D56;&#x4E86; nirvana &#x5305;&#x548C;&#x5176;&#x4ED6;&#x7B2C;&#x4E09;&#x65B9;&#x7684;&#x5305;&#xFF0C;&#x5B9E;&#x9645;&#x4E0A;&#x662F;&#x4E00;&#x4E2A;&#x501F;&#x52A9;&#x4E86; Nirvana &#x548C;&#x7B2C;&#x4E09;&#x65B9;&#x529F;&#x80FD;&#x7684;&#x6269;&#x5C55;&#xFF09;&#x3002;</p>
<p>Server &#x63A5;&#x53E3;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-comment">// Server is a complete API server.</span>
<span class="hljs-comment">// The server contains a router to handle all requests form clients.</span>
<span class="hljs-keyword">type</span> Server <span class="hljs-keyword">interface</span> {
    <span class="hljs-comment">// Serve starts to listen and serve requests.</span>
    <span class="hljs-comment">// The method won&apos;t return except an error occurs.</span>
    Serve() error
    <span class="hljs-comment">// Shutdown gracefully shuts down the server without interrupting any</span>
    <span class="hljs-comment">// active connections.</span>
    Shutdown(ctx context.Context) error
    <span class="hljs-comment">// Builder create a service builder for current server. Don&apos;t use this method directly except</span>
    <span class="hljs-comment">// there is a special server to hold http services. After server shutdown, clean resources via</span>
    <span class="hljs-comment">// returned cleaner.</span>
    <span class="hljs-comment">// This method always returns same builder until cleaner is called. Then it will</span>
    <span class="hljs-comment">// returns new one.</span>
    Builder() (builder service.Builder, cleaner <span class="hljs-keyword">func</span>() error, err error)
}
</code></pre>
<p>&#x521B;&#x5EFA; Server &#x9700;&#x8981;&#x4F7F;&#x7528; Config&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-comment">// Config describes configuration of server.</span>
<span class="hljs-keyword">type</span> Config <span class="hljs-keyword">struct</span> {
    ...
}

<span class="hljs-comment">// Locked checks if the config is locked.</span>
<span class="hljs-keyword">func</span> (c *Config) Locked() <span class="hljs-keyword">bool</span>

<span class="hljs-comment">// IP returns listenning ip.</span>
<span class="hljs-keyword">func</span> (c *Config) IP() <span class="hljs-keyword">string</span>

<span class="hljs-comment">// Port returns listenning port.</span>
<span class="hljs-keyword">func</span> (c *Config) Port() <span class="hljs-keyword">uint16</span>

<span class="hljs-comment">// Logger returns logger.</span>
<span class="hljs-keyword">func</span> (c *Config) Logger() log.Logger

<span class="hljs-comment">// Configurer is used to configure server config.</span>
<span class="hljs-keyword">type</span> Configurer <span class="hljs-keyword">func</span>(c *Config) error

<span class="hljs-comment">// Configure configs by configurers. It panics if an error occurs or config is locked.</span>
<span class="hljs-keyword">func</span> (c *Config) Configure(configurers ...Configurer) *Config

<span class="hljs-comment">// Config gets external config by name. This method is for plugins.</span>
<span class="hljs-keyword">func</span> (c *Config) Config(name <span class="hljs-keyword">string</span>) <span class="hljs-keyword">interface</span>{}

<span class="hljs-comment">// Set sets external config by name. This method is for plugins.</span>
<span class="hljs-comment">// Set a nil config will delete it.</span>
<span class="hljs-keyword">func</span> (c *Config) Set(name <span class="hljs-keyword">string</span>, config <span class="hljs-keyword">interface</span>{})

<span class="hljs-comment">// NewServer creates a nirvana server. After creation, don&apos;t modify</span>
<span class="hljs-comment">// config. Also don&apos;t create another server with current config.</span>
<span class="hljs-keyword">func</span> NewServer(c *Config) Server
</code></pre>
<p>&#x5728; Config &#x4E2D;&#xFF0C;&#x5B58;&#x5728;&#x4E00;&#x4E9B; Server &#x7EA7;&#x522B;&#x7684;&#x914D;&#x7F6E;&#xFF0C;&#x8FD9;&#x4E9B;&#x914D;&#x7F6E;&#x662F;&#x9488;&#x5BF9;&#x5F53;&#x524D;&#x670D;&#x52A1;&#x7684;&#x3002;&#x800C;&#x5BF9;&#x5E94;&#x7684; Configurer &#x4E5F;&#x5728;&#x5F53;&#x524D;&#x5305;&#x4E2D;&#x3002;&#x5982;&#x679C;&#x9700;&#x8981;&#x5BF9;&#x914D;&#x7F6E;&#x8FDB;&#x884C;&#x6269;&#x5C55;&#xFF0C;&#x589E;&#x5F3A; Server &#x529F;&#x80FD;&#xFF0C;&#x5219;&#x53EF;&#x4EE5;&#x589E;&#x52A0;&#x76F8;&#x5E94;&#x5B57;&#x6BB5;&#xFF0C;&#x5426;&#x5219;&#x5E94;&#x8BE5;&#x4F7F;&#x7528;&#x63D2;&#x4EF6;&#x673A;&#x5236;&#x589E;&#x52A0;&#x529F;&#x80FD;&#x3002;</p>
<p>&#x5728; Config &#x4E2D;&#x53EF;&#x4EE5;&#x770B;&#x5230; Config &#x548C; Set &#x65B9;&#x6CD5;&#xFF0C;&#x8FD9;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#x5C31;&#x662F;&#x63D0;&#x4F9B;&#x7ED9;&#x63D2;&#x4EF6;&#x5141;&#x8BB8;&#x63D2;&#x4EF6;&#x8BBE;&#x7F6E;&#x81EA;&#x8EAB;&#x7684;&#x914D;&#x7F6E;&#x7684;&#x3002;</p>
<pre><code class="lang-go"><span class="hljs-comment">// ConfigInstaller is used to install config to service builder.</span>
<span class="hljs-keyword">type</span> ConfigInstaller <span class="hljs-keyword">interface</span> {
    <span class="hljs-comment">// Name is the external config name.</span>
    Name() <span class="hljs-keyword">string</span>
    <span class="hljs-comment">// Install installs stuffs before server starting.</span>
    Install(builder service.Builder, config *Config) error
    <span class="hljs-comment">// Uninstall uninstalls stuffs after server terminating.</span>
    Uninstall(builder service.Builder, config *Config) error
}
</code></pre>
<p>&#x6CE8;&#x518C;&#x4E86;&#x63D2;&#x4EF6;&#x4E4B;&#x540E;&#xFF0C;&#x5728;&#x670D;&#x52A1;&#x542F;&#x52A8;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4F1A;&#x904D;&#x5386;&#x6240;&#x6709;&#x63D2;&#x4EF6;&#x914D;&#x7F6E;&#xFF0C;&#x7136;&#x540E;&#x8C03;&#x7528;&#x63D2;&#x4EF6;&#x7684; Install &#x65B9;&#x6CD5;&#x5B89;&#x88C5;&#x63D2;&#x4EF6;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="service.html" class="navigation navigation-prev " aria-label="Previous page: service">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="config.html" class="navigation navigation-next " aria-label="Next page: config">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"nirvana","level":"1.5.10","depth":2,"next":{"title":"config","level":"1.5.11","depth":2,"path":"topics/config.md","ref":"topics/config.md","articles":[]},"previous":{"title":"service","level":"1.5.9","depth":2,"path":"topics/service.md","ref":"topics/service.md","articles":[]},"dir":"ltr"},"config":{"plugins":[],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"language":"zh-hans","gitbook":"*"},"file":{"path":"topics/nirvana.md","mtime":"2019-08-06T09:48:58.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-10-20T10:33:16.586Z"},"basePath":"..","book":{"language":"zh-hans"}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

